package cn.yuanqiao.activiti.aspect;

import cn.yuanqiao.common.core.domain.R;
import cn.yuanqiao.common.core.domain.entity.SysDept;
import cn.yuanqiao.common.core.domain.entity.SysUser;
import cn.yuanqiao.common.utils.SecurityUtils;

import cn.yuanqiao.system.service.ISysDeptService;
import cn.yuanqiao.system.service.ISysUserService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName: ApprovalGroupAspect
 * @Description: 类的描述
 * @author: lihuahua
 * @date: 2023/9/18 17:20
 * @Blog: huahuacoco.top
 */


@Aspect
@Component
public class ApprovalGroupAspect {

    @Autowired
    private ISysUserService sysUserService;


    @Resource
    private ISysDeptService remoteSysDeptService;

    @Before("@annotation(AddApprovalGroup)")
    public void addApprovalGroup(JoinPoint joinPoint) {
        // 在这里添加审批组逻辑
        // 可以根据需要获取审批组信息，并将其添加到入参中
        // 获取方法参数
        Object[] methodArgs = joinPoint.getArgs();

        // 在这里可以处理方法参数，根据需要添加审批组信息
        String tenantCode = SecurityUtils.getLoginUser().getUser().getTenantCode();
        List<Map<String,Object>> roleKeyByTenantCode = sysUserService.selectRoleKeyByTenantCode(tenantCode);
        for (Object arg : methodArgs) {
            if (arg instanceof Map) {
                // 处理参数逻辑
                Map<String,List> roleKeyMap = new HashMap();
                roleKeyByTenantCode.forEach(item->{
                    Object role_key = item.get("ROLE_KEY");
                    if (roleKeyMap.get(role_key)==null){
                        List subList = new ArrayList();
                        subList.add(item.get("USER_NAME"));
                        roleKeyMap.put(String.valueOf(role_key),subList);
                    }else {
                        roleKeyMap.get(role_key).add(item.get("USER_NAME"));
                    }
                });


                for (Map.Entry<String, List> item:roleKeyMap.entrySet()){
                    ((Map) arg).put(item.getKey(),item.getValue());
                }

                SysDept dept = SecurityUtils.getLoginUser().getUser().getDept();
                String daglyRoleKey = dept.getDaglyRoleKey();
                String leader = dept.getLeader();
                List<SysUser> deptLeader = sysUserService.selectSysUserByRoleKey(leader,tenantCode);
                List<SysUser> deptDagly = sysUserService.selectSysUserByRoleKey(daglyRoleKey,tenantCode);
                List<String> leaderList = deptLeader==null?null:deptLeader.stream().map(SysUser::getUserName).collect(Collectors.toList());
                List<String> daglyList = deptDagly==null?null:deptDagly.stream().map(SysUser::getUserName).collect(Collectors.toList());
                String gdcsId = null;
                if (((Map) arg).get("archiveData")!=null){
                    gdcsId = String.valueOf(((Map<String,List<Map<String,Object>>>) arg).get("archiveData").get(0).get("GDCS_ID"));
                }
                String startUser = SecurityUtils.getLoginUser().getUser().getUserName();
                List<String> archiveDeptList = null;
                if (gdcsId!=null&&!gdcsId.equals("")&&!gdcsId.equals("null")){
                    SysDept archiveDept = remoteSysDeptService.selectDeptById(Long.valueOf(gdcsId));
                    if (archiveDept!=null){
                        List<SysUser> archiveDeptLeader = sysUserService.selectSysUserByRoleKey(archiveDept.getLeader(),tenantCode);
                        archiveDeptList = archiveDeptLeader==null?null:archiveDeptLeader.stream().map(SysUser::getUserName).collect(Collectors.toList());
                    }
                }
                ((Map) arg).put("authService",new AuthService(leaderList,daglyList,archiveDeptList,startUser));

                System.out.println("方法前注解");
            }
        }
    }
}
